jQuery(function(){
	
	//获取当前合同的泵数据 服务器分页
    var serverDeviceData = {};
    serverDeviceData.contractID=jQuery('#contractID').val();
    var serverDeviceTable;
    jQuery(document).ready(function () {
	serverDeviceTable = jQuery('#resultDeviceTable').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url":window.rootPath+ '/ui/afterSaleDeviceInfo/getDevice',
            "data": function (d) {
                return $.extend({}, d, serverDeviceData);
            }
        },
        columns: [
            {
            	data: null
            },
            {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildDevice">';
	            	return operate;
	            }   
            },
            {
                data: 'productModel'
            },
            {
            	data: 'productType'
            },
            {
                data: 'productName'
            },
            {
            	data: 'subDivideType'
            },
            {
                data: 'leaveFactoryCode'
            },
            {
                data: 'leaveFactoryDate',
                render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD');
                    else
                        return '';
                }
            }
            ],
        "drawCallback": function (oSettings) {
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                k++;
            }
        }
    });

		//引入合同信息弹窗
		jQuery('#queryContract').on("click",function(){
			jQuery("#queryContractModal").modal('show');
		});

		var serverContractData = {};
		var serverContractTable;
		jQuery(document).ready(function () {
			serverContractTable = jQuery('#resultContractTable').DataTable({
				"serverSide": true,
				"ordering": false,
				"iDisplayLength": 5,
				"ajax": {
					"dataType": "json",
					"type": "get",
					"cache": false,
					"url":window.rootPath+ '/ui/afterSaleDeviceInfo/getContract',
					"data": function (d) {
						return $.extend({}, d, serverContractData);
					}
				},
				columns: [
					{
						data: null
					},
					{
		            	data : null,
						defaultContent : "",
						render : function(data,type, full, meta) {
							var operate = '<button type="button" class="btn btn-info btn-sm selectBtn">引入</button>';
							return operate;
						}
		            },
					{
						data: 'supplierCode'
					},
					{
						data: 'signedDate',
					render: function (data, type, full, meta) {
					if (data)
						return moment(data).format('YYYY/MM/DD');
					else
						return '';
						}
			        },
					{
						data: 'orderUnit'
					},
					{
						data: 'projectName'
					},
					{
						data: 'orderAddress'
					}
				],
				"drawCallback": function (oSettings) {
					var k = 0;
					var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

					for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
						$('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
						k++;
					}
					serverContractTable.off('click', 'tr');
					serverContractTable.on('click','tr',function(event) {
				    event = event|| window.event;
					var target = event.target|| event.scrElement;
					var data = serverContractTable.row(this).data();
					if (target.className.indexOf('selectBtn') != -1) {
						selectContract(data);
					}
					return;
					});
				}
			});
		});
		
		//选择合同
	    var selectContract=function(data){
	    	jQuery(".selectBtn").attr("disabled", true);
			var contractID =data.id;
			jQuery("#contractID").val(data.id);
			jQuery("#supplierCode").val(data.supplierCode);
			jQuery("#orderUnit").val(data.orderUnit);
			var signedDate = moment(data.signedDate).format("YYYY-MM-DD");
			jQuery('#signedDate').val(signedDate);
			jQuery("#projectName").val(data.projectName);
			jQuery("#orderAddress").val(data.orderAddress);
			jQuery("#queryContractModal").modal('hide');
			var url=window.rootPath+"/ui/afterSaleDeviceInfo/initialDevice/"+contractID;
			jQuery.ajax({
				type : 'get',
				url : encodeURI(url),
				dataType : 'json',
				contentType : 'application/json;charset=utf-8',
				success : function(data) {
					serverDeviceData.contractID = contractID;
					serverDeviceTable.draw();
					jQuery(".selectBtn").attr("disabled", false);
				}
			});
	    }

		/*//引入合同信息
		jQuery('#pull-in').on("click",function(){
			var selects=getSelect("#resultContractTable",6);
			if(selects.length>1){
				$.springLemonDialog({
					content: '只能操作一条记录',// 确认文本
					clickButton: function (sender, modal, index) {// 按钮事件处理
						$(this).closeDialog(modal);
					}
				});
			}
			else if(selects.length==0){
				$.springLemonDialog({
					content: '请选择要操作的记录',// 确认文本
					clickButton: function (sender, modal, index) {// 按钮事件处理
						$(this).closeDialog(modal);
					}
				});
			}else if(selects.length==1){
				jQuery("#pull-in").attr("disabled", true);
				var contractID =selects[0].id;
				jQuery("#contractID").val(selects[0].id);
				jQuery("#supplierCode").val(selects[0].supplierCode);
				jQuery("#orderUnit").val(selects[0].orderUnit);
				var signedDate = moment(selects[0].signedDate).format("YYYY-MM-DD");
				jQuery('#signedDate').val(signedDate);
				jQuery("#projectName").val(selects[0].projectName);
				jQuery("#orderAddress").val(selects[0].orderAddress);
				jQuery("#queryContractModal").modal('hide');
				var url=window.rootPath+"/ui/afterSaleDeviceInfo/initialDevice/"+contractID;
				jQuery.ajax({
					type : 'get',
					url : encodeURI(url),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function(data) {
						serverDeviceData.contractID = contractID;
						serverDeviceTable.draw();
						jQuery("#pull-in").attr("disabled", false);
					}
				});
				
			}
		});*/

	//	jQuery('#supplierCode').change(function(){
	//	var supplierCode=jQuery(this).children('option:selected').val();
	//	var url=window.rootPath+"/ui/contract/getContractBySupplierCode"+"?supplierCode="+supplierCode;
	//	jQuery.ajax({
	//		url:  encodeURI(url),
	//		cache:false,
     //       type: 'get',
     //       dataType: 'json',
     //       success: function (data) {
     //       	var contractID = data.contract.id;
	//			var supplierCode=data.contract.supplierCode;
     //       	var signedDate = moment(data.contract.signedDate).format("YYYY-MM-DD");
     //       	var orderUnit = data.contract.orderUnit;
     //       	var projectName = data.contract.projectName;
     //       	var orderAddress = data.contract.orderAddress;
     //       	jQuery('#contractID').val(contractID);
	//			jQuery('#supplierCode').val(supplierCode);
     //       	jQuery('#signedDate').val(signedDate);
     //       	jQuery('#orderUnit').val(orderUnit);
     //       	jQuery('#projectName').val(projectName);
     //       	jQuery('#orderAddress').val(orderAddress);
     //       	serverDeviceData.contractID = contractID;
     //           serverDeviceTable.draw();
     //       }
     //   });
	//
	//})
	
    /////编辑设备出厂信息
	   jQuery('.editDeviceBtn').on('click', function () {
		   var selects=getSelect("#resultDeviceTable",1);
		   if (selects.length > 1) {
				$.springLemonDialog({
					content : '一次只能编辑一条记录',// 确认文本
					clickButton : function(sender, modal, index) {// 按钮事件处理
						$(this).closeDialog(modal);
					}
				});
			} else if (selects.length == 0) {
				$.springLemonDialog({
					content : '请选择要编辑的记录',// 确认文本
					clickButton : function(sender, modal, index) {// 按钮事件处理
						$(this).closeDialog(modal);
					}
				});
			} else if (selects.length == 1) {
				var id = selects[0].id;
				jQuery("#deviceID").val(id);
				jQuery("#leaveFactoryCode").val(selects[0].leaveFactoryCode);
				if (selects[0].leaveFactoryDate) {
				var time = moment(selects[0].leaveFactoryDate).format('YYYY-MM-DD');
				jQuery('#leaveFactoryDate').val(time);
			    }
				jQuery('#editDeviceModal').modal('show');
			}
	   });
	   
	 //设备出厂编号输入框失去焦点后验证方案编号有效性
	    jQuery("#leaveFactoryCode").blur( function () { 
	    	var deviceID=jQuery("#deviceID").val();
	    	var leaveFactoryCode=jQuery("#leaveFactoryCode").val();
	    	if (leaveFactoryCode!=''){
	    		var urlValid=window.rootPath+"/ui/afterSaleDeviceInfo/validLeaveFactoryCode/"+deviceID+"/"+leaveFactoryCode;
		    	jQuery.ajax({
		            url:encodeURI(urlValid),
		            cache:false,
		            type :"get",
		            dataType:"json",
		            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
		            success:function(data){
		            	if(data.state==1){
		            		alert("设备出厂编号已存在，请修改！");
		            	}
		            }
		    	});
	    	}
	    });
	
	//保存设备出厂信息
	var $btn = jQuery('.saveDeviceBtn').on('click', function () {
		if(jQuery("#editDeviceAtrrForm") && !jQuery("#editDeviceAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
            return;
        };
		var id = jQuery("#deviceID").val();
		var leaveFactoryCode = jQuery("#leaveFactoryCode").val();
		var leaveFactoryDate = jQuery("#leaveFactoryDate").val();
		var contractID = jQuery('#contractID').val();
		jQuery(".saveDeviceBtn").attr("disabled", true);
		var urlValid=window.rootPath+"/ui/afterSaleDeviceInfo/validLeaveFactoryCode/"+id+"/"+leaveFactoryCode;
    	jQuery.ajax({
            url:encodeURI(urlValid),
            cache:false,
            type :"get",
            dataType:"json",
            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
            success:function(data){
            	if(data.state==1){
            		alert("设备出厂编号已存在，请修改！");
            		jQuery(".saveDeviceBtn").attr("disabled", false);
            		return;
            	}
            	var url=window.rootPath+"/ui/afterSaleDeviceInfo/saveDevice";
        		var d = {
        				"id" : id,
        				"leaveFactoryCode" : leaveFactoryCode,
        				"leaveFactoryDate": leaveFactoryDate,
        				"contractID" : contractID
        		};
        		jQuery.ajax({
        			type : 'post',
        			url : encodeURI(url),
        			data : JSON.stringify(d),
        			dataType : 'json',
        			contentType : 'application/json;charset=utf-8',
        			success : function(data) {
        				jQuery("#editDeviceModal").modal('hide');
        				serverDeviceTable.draw(true);
        				jQuery(".saveDeviceBtn").attr("disabled", false);
        			}
        		});
            }
    	});
	});
	
	//列表中的全选
    $("#checkAllDevice").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchildDevice").prop("checked", check);
	});

		//查询
		jQuery('.searchContractBtn').on('click',function(){
			serverContractData.key=jQuery('#keywordContract').val();
			serverContractTable.draw();
		})
		//撤销查询
		jQuery('.resetContractBtn').on("click",function()
		{
			jQuery('#keywordContract').val('');
			serverContractData.key ='';
			serverContractTable.draw();
		});
	
});
});
